
    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>runnbale和callable的区别 - 学习卡片</title>
      <style>
        body { font-family: sans-serif; background-color: #f0f8ff; color: #333; display: flex; flex-direction: column; align-items: center; padding: 50px 20px; }
        .header h1 { font-size: 32px; }
        .grid-container { display: grid; grid-template-columns: repeat(3, 1fr); gap: 28px; width: 100%; max-width: 1200px; }
        .card-container { perspective: 1200px; cursor: pointer; height: 250px; }
        .card { width: 100%; height: 100%; position: relative; transform-style: preserve-3d; transition: transform 0.7s; border-radius: 16px; box-shadow: 0 4px 16px rgba(0,0,0,0.08); }
        .card-container.flipped .card { transform: rotateY(180deg); }
        .card-face { position: absolute; width: 100%; height: 100%; backface-visibility: hidden; display: flex; flex-direction: column; box-sizing: border-box; border-radius: 16px; background-color: #fff; padding: 24px; }
        .card-back { background-color: #f0fff4; transform: rotateY(180deg); justify-content: space-between; }
        .card-category { font-size: 14px; color: #0052d9; margin-bottom: 8px; font-weight: 500; }
        .card-question { font-size: 20px; font-weight: 500; flex-grow: 1; display: flex; align-items: center; justify-content: center; text-align: center; }
        .card-answer-wrapper { flex-grow: 1; overflow-y: auto; }
        .card-answer { font-size: 15px; line-height: 1.7; }
        .card-footer { font-size: 13px; color: #8a919f; border-top: 1px solid #f0f0f0; padding-top: 16px; margin-top: 16px; }
        .card-source { font-size: 13px; color: #8a919f; border-top: 1px solid #f0f0f0; padding-top: 12px; margin-top: 12px; }
      </style>
    </head>
    <body>
      <div class="header">
        <h1>runnbale和callable的区别 - 学习卡片</h1>
      </div>
      <div class="grid-container">
        
    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">特性</div>
          <div class="card-question">Java中的`Runnable`和`Callable`在返回值方面最核心的区别是什么？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">特性</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">`Runnable`接口的`run()`方法不返回任何结果（其返回类型为void）。而`Callable`接口的`call()`方法可以返回一个泛型结果，并且能够抛出异常。</div>
          </div>
          <div class="card-source">来源: 1. 返回值</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">机制</div>
          <div class="card-question">在处理异常方面，`Runnable`的`run()`方法与`Callable`的`call()`方法有何不同？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">机制</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">`Runnable`的`run()`方法不能抛出受检异常，所有受检异常必须在方法内部捕获处理。相比之下，`Callable`的`call()`方法签名中声明了可以抛出`Exception`，允许将受检异常向上抛出，由调用方处理。</div>
          </div>
          <div class="card-source">来源: 2. 异常处理</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">技术</div>
          <div class="card-question">如何执行一个`Callable`任务并获取其返回结果？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">技术</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">`Callable`任务不能直接传递给`Thread`类执行，它需要通过`ExecutorService`的`submit()`方法提交。该方法会返回一个`Future`对象，通过这个`Future`对象可以异步获取`Callable`任务的执行结果。</div>
          </div>
          <div class="card-source">来源: 3. 使用场景</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">对比</div>
          <div class="card-question">`Runnable`和`Callable`各自典型的适用场景是什么？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">对比</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">`Runnable`适合执行那些不需要返回结果的简单任务，例如打印日志、更新某个状态等。`Callable`则适合执行需要返回计算结果或查询结果的任务，例如数值计算或数据库查询。</div>
          </div>
          <div class="card-source">来源: 1. 返回值</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">特性</div>
          <div class="card-question">请写出`Runnable`和`Callable`接口中核心方法的定义（方法签名）。</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">特性</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">`Runnable`接口的核心方法定义是 `void run()`。`Callable`接口的核心方法定义是 `T call() throws Exception`。</div>
          </div>
          <div class="card-source">来源: 1. 返回值</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">机制</div>
          <div class="card-question">如果将一个`Runnable`任务通过`ExecutorService`的`submit()`方法提交，返回的`Future`对象有什么特点？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">机制</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">当一个`Runnable`任务被提交给`ExecutorService`的`submit()`方法时，虽然也会返回一个`Future`对象，但当任务成功完成后，调用该`Future`对象的`get()`方法将返回`null`。</div>
          </div>
          <div class="card-source">来源: 4. 与线程池的结合</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">技术</div>
          <div class="card-question">为什么说`Callable`在处理任务执行错误时比`Runnable`更灵活？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">技术</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">因为`Callable`的`call()`方法可以直接抛出受检异常，这使得调用方可以通过捕获这些异常来更直接、更灵活地处理任务执行过程中发生的错误情况，而`Runnable`则必须在内部处理异常。</div>
          </div>
          <div class="card-source">来源: 2. 异常处理</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">对比</div>
          <div class="card-question">`Runnable`和`Callable`在与`Thread`类的直接交互上有何差异？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">对比</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">`Runnable`实例可以直接作为参数传递给`Thread`类的构造函数来创建一个新线程。而`Callable`实例不能直接传递给`Thread`类，它必须通过`ExecutorService`来执行。</div>
          </div>
          <div class="card-source">来源: 3. 使用场景</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">模式</div>
          <div class="card-question">在并发编程中，`Callable`通常与哪个接口结合使用来获取异步任务的结果？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">模式</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">`Callable`通常与`Future`接口结合使用。`Callable`任务被提交给线程池后会返回一个`Future`对象，该对象代表了异步计算的结果，可以通过它来检查任务是否完成并获取其返回值。</div>
          </div>
          <div class="card-source">来源: 3. 使用场景</div>
        </div>
      </div>
    </div>

      </div>
    </body>
    </html>
